from wineArea import Area

class AppellationArea(Area):
    _interActiveArea = False
    loadOnInit = False    
    _debugPrefix = '    a '

    def dumpSystemSubArea(self,i):        
        return None if self._system_cepages is self.parentArea._system_cepages else self._system_cepages

    def readSystemData(self,c):
        if c :
            self._system_cepages = c

    def getCountry(self):
        reg = self.getRegion()
        if reg:
            return reg.getCountry()
        return None
    
    def getRegion(self):
        if isinstance(RegionArea, self.parentArea):
            return self.parentArea
        elif self.parentArea:
            return self.parentArea.getRegion()
        return None

    def getRegionOrAbove(self):
        p = self.parentArea
        if isinstance(p, GroupAppellationArea):
            return p.getRegionOrAbove()
        return p

    def __getitem__(self, w):
        return self.getWine(w)

    def toDict(self,user=False):
        d=super(AppellationArea,self).toDict(user)
        d['isApp'] = True
        return d




class GroupAppellationArea(Area):
    _interActiveArea = False        
    _debugPrefix = '    ga '
    def dumpSystemSubArea(self,i):
        pre = '    '*i
        s1 = '{\n'+pre+'"cepages" : '+unicode(None if self._system_cepages is self.parentArea._system_cepages else self._system_cepages)+',\n'
        s2 = pre+' "areas"   : ' + RegionArea.dumpSystemSubArea( self ,i+1)
        return s1+s2+'}\n'
    
    def readSystemData(self,dic):
        c = dic['cepages']
        self.localname = dic.get('localname',self.name)
        if c : self._system_cepages = c
        for aname, v in dic['areas'].iteritems():
            area = AppellationArea(name = aname, parentArea=self)
            area.readSystemData(v)
            self.addSubArea(area,addUserArea=False)

    def getCountry(self):
        if self.parentArea :            
            return self.parentArea.getCountry()
        return None
    def getRegion(self):
        return self.parentArea 

    def getRegionOrAbove(self):
        p = self.parentArea
        return p
